<script>
  let getStrBytes = function (str) {
    if (str == null || str == undefined) return 0
    if (typeof str != "string") return 0
    var total = 0, charCode, i, len
    for (var i = 0, len = str.length; i < len; i++) {
      charCode = str.charCodeAt(i)
      if (charCode <= 0x007f) {
        total += 1 //字符代码在 000000 - 00007F之间的，用一个字节编码
      } else if (charCode <= 0x07ff) {
        total += 2//000080 - 0007FF之间的字符用两个字节
      } else if (charCode <= 0xffff) {
        total += 3//000800 - 00D7FF 和 00E00 - 00FFFF之间的，用三个字节
      } else {
        total += 4//010000 - 10FFFF之间的用4个字节
      }
    }
    return total
  }
  console.log(getStrBytes("js 获取字母所占字节数（UTE-8编码）"))
</script>